package xj.model.uml;

import static xj.model.uml.UMLModelConstants.ROLE_STATE;
import static xj.model.uml.UMLModelConstants.ROLE_TRANSITION;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HyperState extends State {

  public HyperState() {
    this(null);
  }

  public HyperState(String name) {
    super(name, StateType.Hyper_State);
  }

  protected HyperState(String name, StateType type) {
    super(name, type);
  }

  public void addState(State s) {
    if (s != null) {
      states.add(s);
      addOwnedElement(s, ROLE_STATE);
    }
  }

  public void removeState(State s) {
    if (s != null) {
      states.remove(s);
      removeOwnedElement(s);
    }
  }

  public Set<State> getStates() {
    return states;
  }

  public State getState(String name) {
    if (name != null) {
      Iterator<State> iter = states.iterator();
      while (iter.hasNext()) {
	State s = iter.next();
	if (name.equals(s.getName())) {
	  return s;
	}
      }
    }
    return null;
  }

  public void addTransition(Transition t) {
    if (t != null) {
      transitions.add(t);
      addOwnedElement(t, ROLE_TRANSITION);
    }
  }

  public void removeTransition(Transition t) {
    if (t != null) {
      transitions.remove(t);
      removeOwnedElement(t);
    }
  }

  public Set<Transition> getTransitions() {
    return transitions;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    HyperState hs = (HyperState) super.clone();

    hs.states = new HashSet<State>();
    Iterator<State> iter1 = states.iterator();
    while (iter1.hasNext()) {
      State s = iter1.next();
      hs.addState((State) s.clone());
    }

    hs.transitions = new HashSet<Transition>();
    Iterator<Transition> iter2 = transitions.iterator();
    while (iter2.hasNext()) {
      Transition t = iter2.next();
      hs.addTransition((Transition) t.clone());
    }

    return hs;
  }

  protected Set<State> states = new HashSet<State>();

  protected Set<Transition> transitions = new HashSet<Transition>();

}
